package com.service.system.impl;

import com.dao.system.ModuleDao;
import com.dao.system.RoleDao;
import com.domain.system.Role;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.system.RoleService;
import com.utils.MD5Util;
import com.utils.MapperUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class RoleServiceImpl implements RoleService {
    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Role> findAll() {
        List<Role> list = null;
        try {
            SqlSession session= MapperUtils.init();
            RoleDao mapper = session.getMapper(RoleDao.class);
            list = mapper.findAll();
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo findAll(int page, int size) {
        PageInfo pageInfo = null;
        try {
            SqlSession session = MapperUtils.init();
            RoleDao mapper = session.getMapper(RoleDao.class);
            Page pageHelper = PageHelper.startPage(page,size);
            mapper.findAll();
            pageInfo = new PageInfo(pageHelper);
            //释放资源
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @Override
    public Role findById(String id) {
        Role role = null;
        try {
            SqlSession session = MapperUtils.init();
            RoleDao mapper = session.getMapper(RoleDao.class);
            role = mapper.findById(id);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return role;
    }

    /**
     * 保存
     * @param role
     */
    @Override
    public void save(Role role) {
        try {
            SqlSession session = MapperUtils.init();
            RoleDao mapper = session.getMapper(RoleDao.class);
            String id = UUID.randomUUID().toString();
            role.setId(id);
            mapper.save(role);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除
     * @param role
     */
    @Override
    public void delete(Role role) {
        try {
            SqlSession session = MapperUtils.init();
            RoleDao mapper = session.getMapper(RoleDao.class);
            mapper.delete(role);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改
     * @param role
     */
    @Override
    public void update(Role role) {
        try {
            SqlSession session = MapperUtils.init();
            RoleDao mapper = session.getMapper(RoleDao.class);
            mapper.update(role);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateRoleModule(String roleId, String moduleIds) {
        try {
            SqlSession session = MapperUtils.init();
            RoleDao mapper = session.getMapper(RoleDao.class);
            //将当前角色与模块有的关系删除
            mapper.deleteRoleModule(roleId);

            //因为可能存在有多个关联,所以循环取出
            String[] split = moduleIds.split(",");
            for (String moduleId : split) {
                //建立新的关联
                mapper.saveRoleModule(roleId,moduleId);
            }
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Role> findAllRoleByUserId(String id) {
        List<Role> list = null;
        try {
            SqlSession session= MapperUtils.init();
            RoleDao mapper = session.getMapper(RoleDao.class);
            list = mapper.findAllRoleByUserID(id);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
